package com.cxx.purchasecharge.component.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cxx.purchasecharge.component.bean.PieChartDataBean;
import com.cxx.purchasecharge.core.model.OrderStatusCode;
import com.cxx.purchasecharge.core.model.OrderTypeCode;
import com.cxx.purchasecharge.core.model.persistence.Order;
import com.cxx.purchasecharge.core.model.persistence.OrderItem;
import com.cxx.purchasecharge.core.util.DateUtils;
import com.cxx.purchasecharge.dal.OrderDao;
import com.cxx.purchasecharge.dal.OrderItemDao;

@Controller
@RequestMapping ("/report")
public class GoodsReportManager2
{
    @SuppressWarnings ("unused")
    private static final Logger logger = Logger.getLogger (GoodsReportManager2.class);

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderItemDao orderItemDao;

    @RequestMapping (value = "/goodsReportQuery")
    public String redirectToGoodsReportQuery (ModelMap model)
    {
        return "goodsReportQuery";
    }
    @RequestMapping (value = "/customerReportQuery")
    public String redirectToCustomerReportQuery (ModelMap model)
    {
        return "customerReportQuery";
    }

    @RequestMapping (value = "/generatePieForGoodsSalesVolume", method = RequestMethod.POST)
    public @ResponseBody
    String generatePieForGoodsSalesVolume (@RequestParam String start, @RequestParam String end)
    {
        Date std = DateUtils.string2Date (start + " 00:00:00", DateUtils.DATE_TIME_PATTERN);
        Date endd = DateUtils.string2Date (end + " 23:59:59", DateUtils.DATE_TIME_PATTERN);

        Map <String, Integer> goodsSalesQuantityMap = new HashMap <String, Integer> ();
        Integer count = 0;
        List <Order> orderOutFinishedList = orderDao.findByRangeCreateDate (OrderTypeCode.OUT,
                                                                            OrderStatusCode.FINISHED, std, endd);
        for (Order order : orderOutFinishedList)
        {
            List <OrderItem> oiList = orderItemDao.findByOrder (order.getId ());
            for (OrderItem oi : oiList)
            {
                count += oi.getAmount ();
                String goodsName = oi.getGoods ().getName ();
                if (!goodsSalesQuantityMap.containsKey (goodsName))
                {
                    goodsSalesQuantityMap.put (goodsName, oi.getAmount ());
                }
                else
                {
                    Integer amount = goodsSalesQuantityMap.get (goodsName);
                    goodsSalesQuantityMap.put (goodsName, amount + oi.getAmount ());
                }
            }
        }

        List <Order> orderOutReturnedList = orderDao.findByRangeCreateDate (OrderTypeCode.OUT_RETURN,
                                                                            OrderStatusCode.FINISHED, std, endd);
        for (Order order : orderOutReturnedList)
        {
            List <OrderItem> oiList = orderItemDao.findByOrder (order.getId ());
            for (OrderItem oi : oiList)
            {
                count -= oi.getAmount ();
                String goodsName = oi.getGoods ().getName ();
                if (!goodsSalesQuantityMap.containsKey (goodsName))
                {
                    goodsSalesQuantityMap.put (goodsName, -oi.getAmount ());
                }
                else
                {
                    Integer amount = goodsSalesQuantityMap.get (goodsName);
                    goodsSalesQuantityMap.put (goodsName, amount - oi.getAmount ());
                }
            }
        }

        List <PieChartDataBean> chartDatas = new ArrayList <PieChartDataBean> ();
        for (Map.Entry <String, Integer> entry : goodsSalesQuantityMap.entrySet ())
        {
            PieChartDataBean chartDataBean = new PieChartDataBean ();
            chartDataBean.setName (entry.getKey ());
            chartDataBean.setY (entry.getValue () / (count * 1.00f));
            chartDatas.add (chartDataBean);
        }
        Collections.sort (chartDatas);
        chartDatas.get (0).setSliced (true);

        JSONArray json = JSONArray.fromObject (chartDatas);
        return json.toString ();
    }

    @RequestMapping (value = "/generateColumnForGoodsSalesVolume", method = RequestMethod.POST)
    public @ResponseBody
    String generateColumnForGoodsSalesVolume (@RequestParam String start, @RequestParam String end)
    {
        Date std = DateUtils.string2Date (start + " 00:00:00", DateUtils.DATE_TIME_PATTERN);
        Date endd = DateUtils.string2Date (end + " 23:59:59", DateUtils.DATE_TIME_PATTERN);

        Map <String, Integer> goodsSalesQuantityMap = new HashMap <String, Integer> ();
        List <Order> orderOutFinishedList = orderDao.findByRangeCreateDate (OrderTypeCode.OUT,
                                                                            OrderStatusCode.FINISHED, std, endd);
        for (Order order : orderOutFinishedList)
        {
            List <OrderItem> oiList = orderItemDao.findByOrder (order.getId ());
            for (OrderItem oi : oiList)
            {
                String goodsName = oi.getGoods ().getName ();
                if (!goodsSalesQuantityMap.containsKey (goodsName))
                {
                    goodsSalesQuantityMap.put (goodsName, oi.getAmount ());
                }
                else
                {
                    Integer amount = goodsSalesQuantityMap.get (goodsName);
                    goodsSalesQuantityMap.put (goodsName, amount + oi.getAmount ());
                }
            }
        }

        List <Order> orderOutReturnedList = orderDao.findByRangeCreateDate (OrderTypeCode.OUT_RETURN,
                                                                            OrderStatusCode.FINISHED, std, endd);
        for (Order order : orderOutReturnedList)
        {
            List <OrderItem> oiList = orderItemDao.findByOrder (order.getId ());
            for (OrderItem oi : oiList)
            {
                String goodsName = oi.getGoods ().getName ();
                if (!goodsSalesQuantityMap.containsKey (goodsName))
                {
                    goodsSalesQuantityMap.put (goodsName, -oi.getAmount ());
                }
                else
                {
                    Integer amount = goodsSalesQuantityMap.get (goodsName);
                    goodsSalesQuantityMap.put (goodsName, amount - oi.getAmount ());
                }
            }
        }

        List <PieChartDataBean> chartDatas = new ArrayList <PieChartDataBean> ();
        for (Map.Entry <String, Integer> entry : goodsSalesQuantityMap.entrySet ())
        {
            PieChartDataBean chartDataBean = new PieChartDataBean ();
            chartDataBean.setName (entry.getKey ());
            chartDataBean.setY (entry.getValue ());
            chartDatas.add (chartDataBean);
        }

        JSONArray json = JSONArray.fromObject (chartDatas);
        return json.toString ();
    }

    @RequestMapping (value = "/generatePieForCustomerSalesVolume", method = RequestMethod.POST)
    public @ResponseBody
    String generatePieForCustomerSalesVolume (@RequestParam String start, @RequestParam String end)
    {
        Date std = DateUtils.string2Date (start + " 00:00:00", DateUtils.DATE_TIME_PATTERN);
        Date endd = DateUtils.string2Date (end + " 23:59:59", DateUtils.DATE_TIME_PATTERN);

        Map <String, Float> customerSalesMoneyMap = new HashMap <String, Float> ();
        Float count = 0f;
        List <Order> orderOutFinishedList = orderDao.findByRangeCreateDate (OrderTypeCode.OUT,
                                                                            OrderStatusCode.FINISHED, std, endd);
        for (Order order : orderOutFinishedList)
        {
            // count += order.getReceivable ();
            count += order.getDealMoney ();
            String customerName = order.getCustomer ().getShortName ();
            if (!customerSalesMoneyMap.containsKey (customerName))
            {
                // customerSalesMoneyMap.put (customerName, order.getReceivable
                // ());
                customerSalesMoneyMap.put (customerName, order.getDealMoney ());
            }
            else
            {
                Float orderMoney = customerSalesMoneyMap.get (customerName);
                // customerSalesMoneyMap.put (customerName, orderMoney +
                // order.getReceivable ());
                customerSalesMoneyMap.put (customerName, orderMoney + order.getDealMoney ());
            }
        }

        List <Order> orderOutReturnedList = orderDao.findByRangeCreateDate (OrderTypeCode.OUT_RETURN,
                                                                            OrderStatusCode.FINISHED, std, endd);
        for (Order order : orderOutReturnedList)
        {
            // count -= order.getReceivable ();
            count -= order.getDealMoney ();
            String customerName = order.getCustomer ().getShortName ();
            if (!customerSalesMoneyMap.containsKey (customerName))
            {
                // customerSalesMoneyMap.put(customerName, -order.getReceivable
                // ());
                customerSalesMoneyMap.put (customerName, -order.getDealMoney ());
            }
            else
            {
                Float orderMoney = customerSalesMoneyMap.get (customerName);
                // customerSalesMoneyMap.put (customerName, orderMoney -
                // order.getReceivable ());
                customerSalesMoneyMap.put (customerName, orderMoney - order.getDealMoney ());
            }
        }

        List <PieChartDataBean> chartDatas = new ArrayList <PieChartDataBean> ();
        for (Map.Entry <String, Float> entry : customerSalesMoneyMap.entrySet ())
        {
            PieChartDataBean chartDataBean = new PieChartDataBean ();
            chartDataBean.setName (entry.getKey ());
            chartDataBean.setY (entry.getValue () / (count * 1.00f));
            chartDatas.add (chartDataBean);
        }
        Collections.sort (chartDatas);
        chartDatas.get (0).setSliced (true);

        JSONArray json = JSONArray.fromObject (chartDatas);
        return json.toString ();
    }

    @RequestMapping (value = "/generatePieForCustomerProfitVolume", method = RequestMethod.POST)
    public @ResponseBody
    String generatePieForCustomerProfitVolume (@RequestParam String start, @RequestParam String end)
    {
        Date std = DateUtils.string2Date (start + " 00:00:00", DateUtils.DATE_TIME_PATTERN);
        Date endd = DateUtils.string2Date (end + " 23:59:59", DateUtils.DATE_TIME_PATTERN);

        Map <String, Float> customerSalesMoneyMap = new HashMap <String, Float> ();
        Float count = 0f;
        List <Order> orderOutFinishedList = orderDao.findByRangeCreateDate (OrderTypeCode.OUT,
                                                                            OrderStatusCode.FINISHED, std, endd);
        for (Order order : orderOutFinishedList)
        {
            count += order.getProfit ();
            String customerName = order.getCustomer ().getShortName ();
            if (!customerSalesMoneyMap.containsKey (customerName))
            {
                customerSalesMoneyMap.put (customerName, order.getProfit ());
            }
            else
            {
                Float orderMoney = customerSalesMoneyMap.get (customerName);
                customerSalesMoneyMap.put (customerName, orderMoney + order.getProfit ());
            }
        }

        List <Order> orderOutReturnedList = orderDao.findByRangeCreateDate (OrderTypeCode.OUT_RETURN,
                                                                            OrderStatusCode.FINISHED, std, endd);
        for (Order order : orderOutReturnedList)
        {
            count -= order.getProfit ();
            String customerName = order.getCustomer ().getShortName ();
            if (!customerSalesMoneyMap.containsKey (customerName))
            {
                customerSalesMoneyMap.put (customerName, -order.getProfit ());
            }
            else
            {
                Float orderMoney = customerSalesMoneyMap.get (customerName);
                customerSalesMoneyMap.put (customerName, orderMoney - order.getProfit ());
            }
        }

        List <PieChartDataBean> chartDatas = new ArrayList <PieChartDataBean> ();
        for (Map.Entry <String, Float> entry : customerSalesMoneyMap.entrySet ())
        {
            PieChartDataBean chartDataBean = new PieChartDataBean ();
            chartDataBean.setName (entry.getKey ());
            chartDataBean.setY (entry.getValue () / (count * 1.00f));
            chartDatas.add (chartDataBean);
        }
        Collections.sort (chartDatas);
        chartDatas.get (0).setSliced (true);

        JSONArray json = JSONArray.fromObject (chartDatas);
        return json.toString ();
    }

    @RequestMapping (value = "/generateColumnForCustomerSalesVolume", method = RequestMethod.POST)
    public @ResponseBody
    String generateColumnForCustomerSalesVolume (@RequestParam String start, @RequestParam String end)
    {
        Date std = DateUtils.string2Date (start + " 00:00:00", DateUtils.DATE_TIME_PATTERN);
        Date endd = DateUtils.string2Date (end + " 23:59:59", DateUtils.DATE_TIME_PATTERN);

        Map <String, Float> customerSalesMoneyMap = new HashMap <String, Float> ();
        List <Order> orderOutFinishedList = orderDao.findByRangeCreateDate (OrderTypeCode.OUT,
                                                                            OrderStatusCode.FINISHED, std, endd);
        for (Order order : orderOutFinishedList)
        {
            String customerName = order.getCustomer ().getShortName ();
            if (!customerSalesMoneyMap.containsKey (customerName))
            {
                // customerSalesMoneyMap.put (customerName, order.getReceivable
                // ());
                customerSalesMoneyMap.put (customerName, order.getDealMoney ());
            }
            else
            {
                Float orderMoney = customerSalesMoneyMap.get (customerName);
                // customerSalesMoneyMap.put (customerName, orderMoney +
                // order.getReceivable ());
                customerSalesMoneyMap.put (customerName, orderMoney + order.getDealMoney ());
            }
        }

        List <Order> orderOutReturnedList = orderDao.findByRangeCreateDate (OrderTypeCode.OUT_RETURN,
                                                                            OrderStatusCode.FINISHED, std, endd);
        for (Order order : orderOutReturnedList)
        {
            String customerName = order.getCustomer ().getShortName ();
            if (!customerSalesMoneyMap.containsKey (customerName))
            {
                // customerSalesMoneyMap.put(customerName, -order.getReceivable
                // ());
                customerSalesMoneyMap.put (customerName, -order.getDealMoney ());
            }
            else
            {
                Float orderMoney = customerSalesMoneyMap.get (customerName);
                // customerSalesMoneyMap.put (customerName, orderMoney -
                // order.getReceivable ());
                customerSalesMoneyMap.put (customerName, orderMoney - order.getDealMoney ());
            }
        }

        List <PieChartDataBean> chartDatas = new ArrayList <PieChartDataBean> ();
        for (Map.Entry <String, Float> entry : customerSalesMoneyMap.entrySet ())
        {
            PieChartDataBean chartDataBean = new PieChartDataBean ();
            chartDataBean.setName (entry.getKey ());
            chartDataBean.setY (entry.getValue ());
            chartDatas.add (chartDataBean);
        }

        JSONArray json = JSONArray.fromObject (chartDatas);
        return json.toString ();
    }

    @RequestMapping (value = "/generateColumnForCustomerProfitVolume", method = RequestMethod.POST)
    public @ResponseBody
    String generateColumnForCustomerProfitVolume (@RequestParam String start, @RequestParam String end)
    {
        Date std = DateUtils.string2Date (start + " 00:00:00", DateUtils.DATE_TIME_PATTERN);
        Date endd = DateUtils.string2Date (end + " 23:59:59", DateUtils.DATE_TIME_PATTERN);

        Map <String, Float> customerSalesMoneyMap = new HashMap <String, Float> ();
        List <Order> orderOutFinishedList = orderDao.findByRangeCreateDate (OrderTypeCode.OUT,
                                                                            OrderStatusCode.FINISHED, std, endd);
        for (Order order : orderOutFinishedList)
        {
            String customerName = order.getCustomer ().getShortName ();
            if (!customerSalesMoneyMap.containsKey (customerName))
            {
                customerSalesMoneyMap.put (customerName, order.getProfit ());
            }
            else
            {
                Float orderMoney = customerSalesMoneyMap.get (customerName);
                customerSalesMoneyMap.put (customerName, orderMoney + order.getProfit ());
            }
        }

        List <Order> orderOutReturnedList = orderDao.findByRangeCreateDate (OrderTypeCode.OUT_RETURN,
                                                                            OrderStatusCode.FINISHED, std, endd);
        for (Order order : orderOutReturnedList)
        {
            String customerName = order.getCustomer ().getShortName ();
            if (!customerSalesMoneyMap.containsKey (customerName))
            {
                customerSalesMoneyMap.put (customerName, -order.getProfit ());
            }
            else
            {
                Float orderMoney = customerSalesMoneyMap.get (customerName);
                customerSalesMoneyMap.put (customerName, orderMoney - order.getProfit ());
            }
        }

        List <PieChartDataBean> chartDatas = new ArrayList <PieChartDataBean> ();
        for (Map.Entry <String, Float> entry : customerSalesMoneyMap.entrySet ())
        {
            PieChartDataBean chartDataBean = new PieChartDataBean ();
            chartDataBean.setName (entry.getKey ());
            chartDataBean.setY (entry.getValue ());
            chartDatas.add (chartDataBean);
        }

        JSONArray json = JSONArray.fromObject (chartDatas);
        return json.toString ();
    }

}
